# 1.定义类
class Car:
    # 初始化
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0

    # 描述方法
    def describe(self):
        print(f"This car is a {self.year} {self.make} {self.model} {self.odometer_reading}.")

    # 读取里程
    def read_odometer(self):
        print(self.odometer_reading)

    # 修改里程数
    def modify_odometer(self, mileage):
        self.odometer_reading += mileage

new_car = Car('AUDI', 'A9', 2025)
new_car.read_odometer()
new_car.describe()


# 2.修改属性的值
new_car.odometer_reading = 10
new_car.describe()


# 3.通过方法修改参数的值
new_car.modify_odometer(20)
new_car.describe()


# 4.继承 -> 子类继承父类首先需要调用父类的 __init__ 方法
class ElectricCar(Car):
    def __init__(self, make, model, year, demo):
        super().__init__(make, model, year) # 初始化父类的 __init__ 方法
        self.demo = 'xxx'
        self.odometer_reading = 10

    def describe(self):
        print(f"This ElectricCar is a {self.make} {self.model}  {self.year} {self.demo}.")

    # 重写父类的方法
    def modify_odometer(self, mileage):
        self.odometer_reading += mileage
        print(self.odometer_reading)
        print("This ElectricCar has been modified.")

new_electricCar = ElectricCar('AUDI', 'A9', 2025, 2026)
new_electricCar.describe()
new_electricCar.modify_odometer(100)


# 5.将实例用作属性
class Battery:
    def __init__(self, battery_size=70):
        self.battery_size = battery_size

    def describe_battery(self):
        print(f"This Battery is {self.battery_size}.")

class ElectricCCar:
    def __init__(self, battery, car):
        self.battery = battery
        self.car = car
        self.battery = Battery()

new_electricCar = ElectricCCar(Battery, ElectricCar)
new_electricCar.battery.describe_battery()


# 6.导入类
import Unit9_1
# from Unit9_1 import ElectricCCar1, Battery1 # 导入模块中指定的类
new_electricCar = Unit9_1.ElectricCCar1("1", "2")
new_electricCar.battery.describe_battery()
